package com.ymatou.iapi.spu.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productspu.parameter.SPUDTO;
import com.ymatou.iapi.productspu.parameter.SyncPspSpuProductBean;
import com.ymatou.iapi.productspu.service.SyncPspSpuProductCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
/**
*执行同步指令
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SyncPspSpuProduct{
	private static SyncPspSpuProductBean syncpspspuproductBean;
	private static SyncPspSpuProductCall syncpspspuproductCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("执行同步指令");
	}

	@Before
	public void caseUp() {
		syncpspspuproductBean=new SyncPspSpuProductBean();
		syncpspspuproductCall=new SyncPspSpuProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syncpspspuproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(String ProductId)  throws InterruptedException {
		Thread.sleep(2000);
		SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
		Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
		Integer spuId=(Integer) spuinfo.get("SpuId");
		Map pinfo=SpuPropertyWapper.selectProductInfoByProductId(ProductId);
//		Logger.debug(spuinfo);
//		Logger.debug(pinfo);
		Logger.verifyEquals(spuinfo.get("BarCode")==null?null:spuinfo.get("BarCode"),pinfo.get("BarCode"),"验证BarCode");
		Logger.verifyEquals(spuinfo.get("BrandEnName")==null?null:spuinfo.get("BrandEnName"),pinfo.get("sBrandEn"),"验证BrandEnName");
		Logger.verifyEquals(spuinfo.get("BrandId")==null?0:spuinfo.get("BrandId"),pinfo.get("iBrandId"),"验证BrandId");
		Logger.verifyEquals(spuinfo.get("BrandName")==null?null:spuinfo.get("BrandName"),pinfo.get("sBrand"),"验证BrandName");

//		Logger.verifyEquals(spuinfo.get("IsPspSpu")==null?null:spuinfo.get("IsPspSpu"),spu.getIsPspSpu(),"验证IsPspSpu");
//		Logger.verifyEquals(spuinfo.get("IsRecmd")==null?false:spuinfo.get("IsRecmd"),spu.getIsRecmd(),"验证IsRecmd");

		Logger.verifyEquals(spuinfo.get("ListingTime")==null?null:spuinfo.get("ListingTime"),pinfo.get("ListingTime"),"验证ListingTime");
		Logger.verifyEquals(spuinfo.get("MasterCategoryId")==null?null:spuinfo.get("MasterCategoryId"),pinfo.get("iMasterCategory"),"验证MasterCategoryId");
//		Logger.verifyEquals(spuinfo.get("MasterCategoryName")==null?null:spuinfo.get("MasterCategoryName"),pinfo.get("iMasterCategory"),"验证MasterCategoryName");
//		Logger.verifyEquals(spuinfo.get("CountryName")==null?null:spuinfo.get("CountryName"),spu.getOrigin(),"验证Origin");
//		Logger.verifyEquals(spuinfo.get("PriceEnd")==null?null:new BigDecimal(spuinfo.get("PriceEnd").toString()).setScale(2),spu.getPriceEnd().setScale(2),"验证PriceEnd");
//		Logger.verifyEquals(spuinfo.get("PriceStart")==null?null:new BigDecimal(spuinfo.get("PriceStart").toString()).setScale(2),spu.getPriceStart().setScale(2),"验证PriceStart");
		Logger.verifyEquals(spuinfo.get("DescText")==null?null:spuinfo.get("DescText"),pinfo.get("WordContent"),"验证ProductDescription");
		Logger.verifyEquals(spuinfo.get("ProductName")==null?null:spuinfo.get("ProductName"),pinfo.get("sProduct"),"验证ProductName");
		Logger.verifyEquals(spuinfo.get("ProductPics")==null?null:spuinfo.get("ProductPics"),pinfo.get("Pictures"),"验证ProductPics");
		Logger.verifyEquals(spuinfo.get("Summary")==null?null:spuinfo.get("Summary"),pinfo.get("Summary"),"验证ProductSummary");
		Logger.verifyEquals(spuinfo.get("SecondCategoryId")==null?null:spuinfo.get("SecondCategoryId"),pinfo.get("iCategoryId"),"验证SecondCategoryId");
//		Logger.verifyEquals(spuinfo.get("SecondCategoryName")==null?null:spuinfo.get("SecondCategoryName"),spu.getSecondCategoryName(),"验证SecondCategoryName");
//		Logger.verifyEquals(spuinfo.get("SpuId")==null?null:spuinfo.get("SpuId"),spu.getSPUId(),"验证SPUId");
		Logger.verifyEquals(spuinfo.get("ThirdCategoryId")==null?null:spuinfo.get("ThirdCategoryId"),pinfo.get("iThirdCategoryId"),"验证ThirdCategoryId");
//		Logger.verifyEquals(spuinfo.get("ThirdCategoryName")==null?null:spuinfo.get("ThirdCategoryName"),spu.getThirdCategoryName(),"验证ThirdCategoryName");
		
		Logger.verifyEquals(spuinfo.get("Unit")==null?null:spuinfo.get("Unit"),pinfo.get("Unit"),"验证Unit");
		Logger.verifyEquals(spuinfo.get("Weight")==null?null:spuinfo.get("Weight"),pinfo.get("Weight"),"验证Weight");
		Logger.verifyEquals(spuinfo.get("Width")==null?null:spuinfo.get("Width"),pinfo.get("Width"),"验证Width");
		Logger.verifyEquals(spuinfo.get("Height")==null?null:spuinfo.get("Height"),pinfo.get("Height"),"验证Height");
		Logger.verifyEquals(spuinfo.get("Length")==null?null:spuinfo.get("Length"),pinfo.get("Length"),"验证Length");
//		
//		List<String> pp = spu.getProductPictures();
//		Collections.sort(pp);
//		List<String> pp2=(List<String>)( spuinfo.get("ProductPics")==null?new ArrayList<>():Arrays.asList(spuinfo.get("ProductPics").toString().split(",")));
//		Collections.sort(pp2);
//		Logger.verifyEquals(pp2.toString(),pp.toString(),"验证ProductPictures");
//		
		List<String> pdp = (List<String>)(pinfo.get("DescPics")==null?new ArrayList<>():Arrays.asList(pinfo.get("DescPics").toString().split(",")));
		Collections.sort(pdp);
		
		List<String> pdp2=(List<String>)( spuinfo.get("DescPics")==null?new ArrayList<>():Arrays.asList(spuinfo.get("DescPics").toString().split(",")));
		Collections.sort(pdp2);
		Logger.verifyEquals(pdp2.toString(),pdp.toString(),"验证ProductDescriptionPics");
//		
		List<Map> pinfo2 = SpuPropertyWapper.selectSpuPropertyInfosBySpuid(spuId);
		List<Map> spinfo=SpuPropertyWapper.selectProductPropertyByProductId(ProductId);
		Logger.debug(pinfo2);
		Logger.debug(spinfo);
		Map<String,Map> mpinfo=new HashMap<>();
		pinfo2.forEach(m->mpinfo.put(m.get("Values").toString(),m));
		Logger.verifyEquals(pinfo2.size(), spinfo.size(), "验证PropertyPairs的size");
		spinfo.forEach(pps->{
			Map mpp = mpinfo.get(pps.get("Value"));
			Logger.verifyEquals(mpp.get("Values")==null?null:mpp.get("Values").toString(), pps.get("Value"), "验证Propert.yName");
			Logger.verifyEquals(mpp.get("PropertyId")==null?null:mpp.get("PropertyId"), pps.get("PropertyId"), "验证PropertyId");
			Logger.verifyEquals(mpp.get("Status")==null?null:mpp.get("Status"), pps.get("Status"), "验证PropertyStatus");
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncPspSpuProduct_001() {
		Logger.start(true,"执行同步指令成功");
		try {
//			ProductManagerCallServiceV2.offSaleAllProduct(3383);
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			syncpspspuproductBean.setProductId(ProductId);
			syncpspspuproductCall.setData(syncpspspuproductBean);
			syncpspspuproductCall.callService();
			Logger.verifyIsContains("ok", syncpspspuproductCall.getReturnData(), "验证返回");
			verifyDB(ProductId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SyncPspSpuProduct_002() {
		Logger.start(true,"不输入ProductId失败");
		try {
			syncpspspuproductCall.setData(syncpspspuproductBean);
			syncpspspuproductCall.callService();			
			Logger.verifyIsContains("ok", syncpspspuproductCall.getReturnData(), "验证返回");
			//接受不处理
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SyncPspSpuProduct_003() {
		Logger.start(true,"非指定维护用户执行同步指令失败");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(288484);
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			syncpspspuproductCall.setData(syncpspspuproductBean);
			syncpspspuproductCall.callService();			
			Logger.verifyIsContains("ok", syncpspspuproductCall.getReturnData(), "验证返回");
			Thread.sleep(5000);
			Map spuinfo =null;
			try{
				spuinfo = new SpuPropertyWapper().selectSpuProductInfoByProductId(ProductId);
			}
			catch (Exception e) {
				Logger.debug(e.getMessage());
			}
			Logger.verifyIsNull(spuinfo, "验证无此数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}